<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="formInline.tableName" placeholder="表名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="find" size="medium" icon="el-icon-search">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="generatorCode" size="medium" :disabled="this.selections.length===0">生成代码</el-button>
      </el-form-item>
    </el-form>
    <v-table :columns="columns" :data="tableData" :loading="loading"
             @findPage="findByPage" @selectionChange="selectionChange" :table-operation="tableOperation">
      <template slot="operation" slot-scope="scope">
        <el-button
          @click="onGenerator(scope.data.row)"
          :size="'mini'"
          type="text">
          生成代码
        </el-button>
      </template>
    </v-table>
  </div>
</template>

<script>
  import {generatorApi} from '../../http/modules/generator'
  import vTable from '../../components/table/table'

  export default {
    name: "generator",
    components: {
      vTable
    },
    data() {
      return {
        tableData: {
          totalSize: 10,
          content: []
        },
        pageRequest: {
          pageNum: 1,
          pageSize: 10
        },
        columns: [
          {prop:"tableName", label:"表名", minWidth:100},
          {prop:"engine", label:"Engine", minWidth:100},
          {prop:"tableComment", label:"表备注", minWidth:120},
          {prop:"createTime", label:"创建时间", minWidth:100},
          ],
        formInline: {
          tableName: ''
        },
        tables: [],
        selections: [],
        loading: true,
        tableOperation: {
          operation: false
        },
      }
    },
    methods: {
      find() {
      },
      generatorCode() {
        this.tables = ''
        this.selections.forEach((item, index) => {
          // this.tables.push(item)
          if (index === 0) {
            this.tables += item.tableName
          } else {
            this.tables +=  "," + item.tableName
          }
        })
        window.location.href = "http://localhost:8090/sys/generator/generatorCode?tables=" + this.tables
        // generatorApi.generatorCode(this.tables).then(data => {
          // console.log(data)
          // let blob = new Blob([data], {type: 'application/zip'})
          // let url = window.URL.createObjectURL(blob)
          // window.location.href = url
          // const blob = new Blob([data], {type: 'application.zip'})
          // // if ('download' in document.createElement)
          // let documentElement = document.createElement('a')
          // documentElement.download = "code"
          // documentElement.style.display = 'none'
          // documentElement.href = URL.createObjectURL(blob)
          // document.body.appendChild(documentElement)
          // documentElement.click()
          // URL.revokeObjectURL(documentElement.href)
          // document.body.removeChild(documentElement)
        // }).catch(error => {
        //   console.log(error)
        // })
        // this.tables = ''
      },
      onGenerator() {

      },
      findByPage(data) {
        if (data != null) {
          this.pageRequest = data
        }
        generatorApi.findPage(this.pageRequest).then(data => {
          this.tableData = data.data
          this.loading = false
        })
      },
      selectionChange(selections) {
        this.selections = selections
      }
    },
    mounted() {
      // this.findByPage(this.pageRequest)
    }
  }
</script>

<style scoped>

</style>
